<template>
    <TabGrayBarCard :title="'订单分析'" @onMore="goDetail()" :tabs="card.tabs" :curTab="card.curTab"
        :tabRight="card.tabRight" @onChange="changeTab" :grayCardList="card.grayCardList"
        :echartId="`myEChartsBar-${props.companyPlatformId}-order`" :echartData="card.echartData" :lineText="''" :barText="'订单金额'"
        :unitText="`单位：${card.unitText}`" :updateText="card.updateText"
        >

    </TabGrayBarCard>
    <!-- v-btnPermi="{ permi: props.permi, noReport: true, isHiden: true }" -->
</template>

<script setup>
import TabGrayBarCard from '@/DbComponents/tabGrayBarCard'
import { toThousands, dateFormat } from '@/utils/number-util'
import dashBoardApi from '@/api/EnterpriseServices/dashBoard'
import { reactive, watch, computed } from 'vue'
// import { reportLog } from '@/utils/reportLog'
import { useRouter } from 'vue-router'
const router = useRouter()
const props = defineProps({
    companyPlatformId: {
        type: String,
        required: false,
        default: ''
    },
    updateDate: {
        type: String,
        required: false,
        default: ''
    }
})
const card = reactive(
    {
        tabs: [{ label: '按月', value: '月' }],
        curTab: '月',
        tabRight: '',
        grayCardList: [{ label: '本月订单', value: '', unit: '', color: '' }, { label: '本年订单', value: '', unit: '', color: '' }, { label: '累计订单', value: '', unit: '', color: '' }],
        unitText: '',
        updateText: '',
        echartData: {},
        startDate: '',
        endDate: '',
    }
)
const getCardData = () => {
    dashBoardApi.orderCardApi({ companyPlatformId: props.companyPlatformId, dataTime: props.updateDate }).then((res) => {
        if (res.data.code == 200) {
            card.tabRight = '截至 ' + res.data.data.timeLab
            card.grayCardList[0].value = toThousands(res.data.data.orderPriceMonth)
            card.grayCardList[1].value = toThousands(res.data.data.orderPriceYear)
            card.grayCardList[2].value = toThousands(res.data.data.orderPriceall)
            // card.grayCardList[2].color = res.data.data.colorLab
            card.unitText = res.data.data.valueUnit
        }
    }).catch(err => {
        console.log(err)
    });
}
const getChartData = () => {
    dashBoardApi.orderChardsApi({ companyPlatformId: props.companyPlatformId, dataTime: props.updateDate }).then((res) => {
        if (res.data.code == 200) {
            card.echartData = res.data.data
        }
    }).catch(err => {
        console.log(err)
    });
}
const goDetail = () => {
    // reportLog({
    //     type: 4,
    //     resourceKey: permi,
    // })

    router.push({
        path: '/page/routeController/index',
        query: {
            rid: 'EnterpriseServices/detailPages/orderAssay/orderAssayDetail',
            rTitle: '订单分析',
            companyPlatformId: props.companyPlatformId,
            startDate: card.startDate,
            endDate: card.endDate,
            updateDate: props.updateDate,
        },
    })
    
}
const changeTab = (value) => {
    // 切换tab，更换开始和结束日期
    // 按天：开始和结束日期，均为【数据更新日期】
    // 按月：开始日期为【数据更新日期】当月第一天，结束日期为【数据更新日期】
    // 按年：开始日期为【数据更新日期】当年第一天，结束日期为【数据更新日期】
    // if (value == '日') {
    //     card.startDate = dateFormat(new Date(new Date(props.updateDate).getTime() - 7*24*60*60*1000).getTime(), 'YYYY-MM-DD')
    //     card.endDate = props.updateDate
    // }
    if (value == '月') {
        const monthsAgo = new Date(props.updateDate)
        monthsAgo.setMonth(monthsAgo.getMonth() - 6)
        card.startDate = dateFormat(monthsAgo, 'YYYY-MM')
        card.endDate = props.updateDate.slice(0,7)
    }
    // if (value == '年') {
    //     const year = new Date(props.updateDate).getFullYear() - 7
    //     card.startDate = year
    //     card.endDate = props.updateDate.slice(0,4)
    // }

    card.curTab = value

    getCardData()
    getChartData()
}

changeTab('月')

const biProjectCode = computed(() => {
    return props.biProjectCode
});
watch(biProjectCode, () => {
    changeTab('月')
}, { deep: true });

</script>

<style scoped lang="scss"></style>
